package com.xtoneict.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xtoneict.auth.entity.Attendance;
import com.xtoneict.auth.entity.LeaveDay;
import com.xtoneict.auth.entity.LeaveDetail;
import com.xtoneict.auth.mapper.AttendanceMapper;
import com.xtoneict.auth.mapper.LeaveDayMapper;
import com.xtoneict.auth.mapper.LeaveDetailMapper;
import com.xtoneict.auth.service.ILeaveDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 为请假的每一天单独构造对象
 */
@Service
public class LeaveDayServiceImpl extends ServiceImpl<LeaveDayMapper, LeaveDay> implements ILeaveDayService {
    @Autowired
    private LeaveDayMapper leaveDayMapper;
    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private LeaveDetailMapper leaveDetailMapper;

    //根据信息选择出请假那天
    public LeaveDay selectLeaveDay(Integer employeeId, Integer yearMonthId, Integer day){
        QueryWrapper<LeaveDay> leaveDayQueryWrapper = new QueryWrapper<>();
        leaveDayQueryWrapper.eq("employee_id", employeeId);
        leaveDayQueryWrapper.eq("year_month_id", yearMonthId);
        leaveDayQueryWrapper.eq("day", day);

        return leaveDayMapper.selectOne(leaveDayQueryWrapper);
    }

    //根据leaveDetail计算存储leaveDay
    public boolean saveLeaveDay(LeaveDetail leaveDetail){
        Attendance attendance = attendanceMapper.selectById(leaveDetail.getAttendanceId());
        String startDate = leaveDetail.getStartDate();
        String endDate = leaveDetail.getEndDate();
        Integer yearMonthId = attendance.getYearMonthId();
        Integer employeeId = attendance.getEmployeeId();
        String startTime = leaveDetail.getStartTime();
        String endTime = leaveDetail.getEndTime();

        try {
            //处理startDate和endDate
            String startDateStr = startDate.substring(3, 5);
            String endDateStr = endDate.substring(3, 5);
            Integer startDay = Integer.parseInt(startDateStr);
            Integer endDay = Integer.parseInt(endDateStr);

            if (startDay == endDay) {
                LeaveDay leaveDay = new LeaveDay();
                leaveDay.setLeaveDetailId(leaveDetail.getLeaveDetailId());
                leaveDay.setDay(startDay);
                leaveDay.setYearMonthId(yearMonthId);
                leaveDay.setEmployeeId(employeeId);

                if (startTime.equals("09:00") && endTime.equals("17:00")) {
                    leaveDay.setHalfDay(false);
                } else {
                    leaveDay.setHalfDay(true);
                }

                leaveDayMapper.insert(leaveDay);
            } else {
                for (int i = startDay; i <= endDay; i++) {
                    LeaveDay leaveDay = new LeaveDay();
                    leaveDay.setLeaveDetailId(leaveDetail.getLeaveDetailId());
                    leaveDay.setDay(i);
                    leaveDay.setYearMonthId(yearMonthId);
                    leaveDay.setEmployeeId(employeeId);

                    if (i == startDay) {
                        if (startTime.equals("09:00") || startTime.equals("00:00")) {
                            leaveDay.setHalfDay(false);
                        } else {
                            leaveDay.setHalfDay(true);
                        }
                    } else if (i == endDay) {
                        if (endTime.equals("17:00") || endTime.equals("23:59")) {
                            leaveDay.setHalfDay(false);
                        } else {
                            leaveDay.setHalfDay(true);
                        }
                    } else {
                        leaveDay.setHalfDay(false);
                    }

                    leaveDayMapper.insert(leaveDay);
                }
            }
        }catch (Exception e){

        }

        return true;
    }
}
